package com.rf.richfitwheel.admin.sys.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.richfitwheel.admin.sys.dao.SqlAdapterMapper;
import com.rf.richfitwheel.admin.sys.dao.SysLogMapper;
import com.rf.richfitwheel.admin.sys.model.SysLog;
import com.rf.richfitwheel.admin.sys.service.SysLogService;
import com.rf.richfitwheel.admin.utils.MapCaseUtil;
import com.rf.richfitwheel.common.utils.PageUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("sysLogService")
public class SysLogServiceImpl implements SysLogService {

	@Autowired
	SysLogMapper sysLogMapper;
	@Autowired
	private SqlAdapterMapper sqlAdapterMapper;//注入dao
	
	@Override
	public PageUtils queryPage(Map<String, Object> params) {

		PageUtils pageUtils=PageUtils.getPageInfo(params);
		Page<Object> p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getLimit());
	    /*StringBuffer  sql=  new StringBuffer("SELECT\n" )
				        .append(" usr.staff_name,\n")
						.append("  usr.username,\n" )
						.append("	slog.description,\n" )
						.append("	slog.methods,\n" )
						.append("	slog.action_time,\n" )
						.append("	slog.user_ip,\n" )
						.append("	date_format(slog.oper_time, '%Y-%m-%d %H:%i:%s') as oper_time\n" )
						.append("FROM\n" )
						.append("	sys_log slog\n" )
						.append("LEFT JOIN sys_user usr ON slog.user_id = usr.id where 1=1  ");

       if(params.get("staff_name")!=null&&!"".equals(params.get("staff_name"))){
		      sql.append("and usr.staff_name like '%").append(params.get("staff_name")).append("%'");
	   }

		if(params.get("username")!=null&&!"".equals(params.get("username"))){
			sql.append("and usr.username = '").append(params.get("username")).append("'");
		}

		if(params.get("user_ip")!=null&&!"".equals(params.get("user_ip"))){
			sql.append("and slog.user_ip = '").append(params.get("user_ip")).append("'");
		}
		if(params.get("oper_time_start")!=null&&!"".equals(params.get("oper_time_start"))){
			sql.append("and slog.oper_time >str_to_date('").append(params.get("oper_time_start")).append("' ,'%Y-%m-%d')");
		}

		if(params.get("oper_time_end")!=null&&!"".equals(params.get("oper_time_end"))){
			sql.append("and slog.oper_time < str_to_date('").append(params.get("oper_time_end")).append("' ,'%Y-%m-%d')");
		}

	    StringBuffer orderSql=new StringBuffer(" ORDER BY   slog.oper_time DESC")	;
		sql.append(orderSql);
	    sqlAdapterMapper.selectSQL(sql.toString());*/
		Map<String, Object> paramsNew = new HashMap<String, Object>();
		if(params.get("staff_name")!=null&&!"".equals(params.get("staff_name"))){
			paramsNew.put("staff_name", "%" + params.get("staff_name").toString() + "%");
		}

		if(params.get("username")!=null&&!"".equals(params.get("username"))){
			paramsNew.put("username", "%" + params.get("username").toString() + "%");
		}

		if(params.get("user_ip")!=null&&!"".equals(params.get("user_ip"))){
			paramsNew.put("user_ip", "%" + params.get("user_ip").toString() + "%");
		}
		
		if(params.get("oper_time_start")!=null&&!"".equals(params.get("oper_time_start"))){
			paramsNew.put("oper_time_start", params.get("oper_time_start").toString() + " 00:00:00");
		}

		if(params.get("oper_time_end")!=null&&!"".equals(params.get("oper_time_end"))){
			paramsNew.put("oper_time_end", params.get("oper_time_end").toString() + " 23:59:59");
		}
		List<Map<String, Object>> list = sysLogMapper.selectByParams(paramsNew);
		pageUtils =new PageUtils(p);
		pageUtils.setList(MapCaseUtil.mapKeyToLowerCase(list));
	    return pageUtils;
	}

	@Override
	public void insertLog(SysLog log) {
		sysLogMapper.insert(log);
	}

}
